window.addEventListener("load", function (){
	var holeWidth = 25, mouseDown;
	var world = document.getElementById("world");
	for(var i = 0; i < 20; i++){
		for(var j = 0; j < 20; j++){
			var hole = createHole(i, j);
			world.appendChild(hole);
		}
	}
	
	function Snake(){
		var running = false, datas = [], dx = 0, dy = 0, eggNum = 0;
		var head = createHole(random(14)+3, random(14)+3, "shead");
		addToSnake(head)
		for(var i = 0; i < 2; i++){
			var sbody = createHole(+datas[datas.length - 1][0], +datas[datas.length - 1][1]+1, "sbody");
			addToSnake(sbody);
			world.appendChild(sbody);
		}
		world.appendChild(head);
	
		function addToSnake(sth){
			datas.push([sth.getAttribute("x")*1, sth.getAttribute("y")*1, sth]);
		}
		
		this.setDir = function(_dx, _dy){
			if(dx != 0 && dx + _dx == 0)return;
			if(dy != 0 && dy + _dy == 0)return;
			dx = _dx, dy = _dy;
		}
		
		this.start = function (){
			running = true;
		}
		
		function move(){
			if(running){
				var next = gameOver(datas[0][0] + dx, datas[0][1] + dy);
				if(next){
					if(next.classList.contains("hole")){
						for(var i = datas.length - 1; i >= 0; i--){
							var sth = datas[i];
							if(i > 0){
								sth[0] = datas[i-1][0];
								sth[1] = datas[i-1][1];
							}else{
								sth[0] = sth[0] + dx;
								sth[1] = sth[1] + dy;
							}
							setPosition(sth[2], sth[0], sth[1]);
						}
					}else if(next.classList.contains("egg")){
						eggNum--;
						datas.unshift([next.getAttribute("x")*1, next.getAttribute("y")*1, next]);
						datas[0][2].className = "shead";
						datas[1][2].className = "sbody";
					}	
				}
			}
		}
		
		function gameOver(){
			var next = geSsth(datas[0][0] + dx, datas[0][1] + dy);
			if(next && next.classList && (next.classList.contains("hole") || next.classList.contains("egg"))){
				return next;
			}else{
				running = false;
				clearInterval(stimer);
				alert("Game over!");
				return null;
			}
		}
		var stimer = setInterval(move, 300);
		
		function addEgg(){
			if(eggNum > 0) return;
			eggNum++;
			while(true){
				var x = random(20), y = random(20);
				var hole = geSsth(x, y);
				if(hole && hole.classList && hole.classList.contains("hole")){
					var egg = createHole(random(20), random(20), "egg");
					world.appendChild(egg);
					break;
				}
			}
		}
		
		var etimer = setInterval(addEgg, 300);
	}
	var snake = new Snake();
	
	function random(i){
		return Math.floor(Math.random() * i);	
	}
	/**
	world.addEventListener("mousemove", function (e){
		if(e.which === 1){
			world.style.cursor = "move";
			var hole = document.elementFromPoint(e.x, e.y);
			if(hole.classList.contains("hole")){
				console.log("create a stone.", hole);
				var space = createHole(hole.getAttribute("i"), hole.getAttribute("j"), "stone");
				world.appendChild(space);
			}
		}else{
			world.style.cursor = "";
		}
	});
	*/
	
	window.addEventListener("keydown", function (e){
		switch(e.keyIdentifier){
			case "Up": snake.setDir(0, -1); break;
			case "Down": snake.setDir(0, 1); break;
			case "Left": snake.setDir(-1, 0); break;
			case "Right": snake.setDir(1, 0); break;
		}
		snake.start();
		return false;
	});
	
	function createHole(x, y, type){
		var hole = document.createElement("div");
		hole.className = type || "hole";
		hole.classList.add("x"+x+"y"+y);
		setPosition(hole, x, y);
		hole.setAttribute("x", x);
		hole.setAttribute("y", y);
		return hole;
	}
	
	function setPosition(sth, x, y){
		with(sth.style){
			left = (x * holeWidth) + "px";
			top = y * holeWidth + "px";
		}
	}
	
	function geSsth(x, y){
		var sth = document.getElementsByClassName("x"+x+"y"+y);
		if(sth.length > 0){
			if(sth.length > 1){	
				console.log(x, y, sth[sth.length - 1]);
			}
			return sth[sth.length - 1];	
		}
		//console.log(x, y, world.offsetLeft + (x + 0.5) * holeWidth, world.offsetTop + (y + 0.5) * holeWidth);
		//return document.elementFromPoint(world.offsetLeft + (x + 0.5) * holeWidth, world.offsetTop + (y + 0.5) * holeWidth);
	}
});